package sg.simple.snmp.provider.impl;

import isnmp.ui.dto.StorageInfo;
import isnmp.ui.dto.StorageInfo.STORAGE_PROPERTY;

import java.util.ArrayList;
import java.util.List;

import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.TableEvent;

public class StorageInfoProvider extends SNMPTableReader {

	/** Conceptual entry to mark the index in table */
	private String storageTableIndexOid = "1.3.6.1.2.1.25.3.3.1";

	private String storageDescriptionOid = "1.3.6.1.2.1.25.2.3.1.3";

	private String storageAllocationUnitsOid = "1.3.6.1.2.1.25.2.3.1.4";

	private String storageSizeInAllocationUnitsOid = "1.3.6.1.2.1.25.2.3.1.5";

	private String storageUsedInAllocationUnitsOid = "1.3.6.1.2.1.25.2.3.1.6";

	private String storageAllocationFailuresOid = "1.3.6.1.2.1.25.2.3.1.7";

	public List<StorageInfo> retrieve(Snmp session, Target target) {

		OID storageIndex = new OID(storageTableIndexOid);
		OID storageDescription = new OID(storageDescriptionOid);
		OID storageAllocationUnits = new OID(storageAllocationUnitsOid);
		OID storageSizeInAllocationUnits = new OID(
				storageSizeInAllocationUnitsOid);
		OID storageUsedInAllocationUnits = new OID(
				storageUsedInAllocationUnitsOid);
		OID storageAllocationFailures = new OID(storageAllocationFailuresOid);

		List<StorageInfo> storageInformationList = new ArrayList<StorageInfo>();

		List<TableEvent> storageInfoTable = this
				.readTable(session, target,
						new OID[] { storageIndex, storageDescription,
								storageAllocationUnits,
								storageSizeInAllocationUnits,
								storageUsedInAllocationUnits,
								storageAllocationFailures });

		for (TableEvent aRow : storageInfoTable) {

			if ( aRow.isError()){
				continue;
			}

			String index = "";
			String description = "";
			String allocationUnits = "";
			String sizeInAllocUnits = "";
			String usedSizeInAllocUnits = "";
			String allocFailures = "";

			for (VariableBinding v : aRow.getColumns()) {
				if (matches(storageIndex, v)) {
					index = v.getVariable().toString();
					continue;
				}

				if (matches(storageDescription, v)) {
					description = v.getVariable().toString();
					continue;
				}
				if (matches(storageAllocationUnits, v)) {
					allocationUnits = v.getVariable().toString();
					continue;
				}

				if (matches(storageSizeInAllocationUnits, v)) {
					sizeInAllocUnits = v.getVariable().toString();
					continue;
				}

				if (matches(storageUsedInAllocationUnits, v)) {
					usedSizeInAllocUnits = v.getVariable().toString();
					continue;
				}

				if (matches(storageAllocationFailures, v)) {
					allocFailures = v.getVariable().toString();
					continue;
				}

			}

			StorageInfo info = new StorageInfo();
			info.putValue(STORAGE_PROPERTY.INDEX, index);
			info.putValue(STORAGE_PROPERTY.DESCRIPTION, description);
			info.putValue(STORAGE_PROPERTY.ALLOCATION_UNIT_SIZE,
					allocationUnits);
			info.putValue(STORAGE_PROPERTY.SIZE, sizeInAllocUnits);
			info.putValue(STORAGE_PROPERTY.USED, usedSizeInAllocUnits);
			info.putValue(STORAGE_PROPERTY.ALLOCATION_FAILURES, allocFailures);

			storageInformationList.add(info);
		}

		return storageInformationList;

	}
}
